package v9.assist.analyst

import android.graphics.Bitmap
import sample.ui.Properties
import kotlinx.coroutines.script.Block
import kotlinx.coroutines.script.TARGET
import kotlinx.coroutines.script.analysis.Analysis
import kotlinx.coroutines.script.SMapBase.hitDeathRed
import kotlinx.coroutines.script.SMapBase.hitEquipO
import kotlinx.coroutines.script.SMapBase.hitSMapEnemyBlue
import kotlinx.coroutines.script.SMapBase.hitSMapEnemyYellow
import kotlinx.coroutines.script.SMapBase.hitTeammate
import kotlinx.coroutines.script.tes.ColorDiffer
import kotlinx.coroutines.script.tes.printLog
import kotlinx.coroutines.script.tes.pt
import kotlinx.coroutines.script.tes.rgb
import kotlinx.coroutines.script.tes.shake
import v9.assist.script.xBossBesideTs
import v9.assist.script.xLastAtkTs
import v9.assist.script.xLastPetBlacksmithTs
import v9.assist.script.xLastSceneEnterTs
import v9.assist.script.xLastUnknownTs
import kotlin.math.abs

private val hitRoad by lazy { // 21-28
    val out = mutableSetOf<ColorDiffer>()
    out.addAll(setOf(0xff393932.toInt().rgb(8),)) // 落地第二段开始. 到最终
    out.addAll(setOf(0xff3e3f3a.toInt().rgb(8),)) // 冰门
    out.addAll(setOf(0xff47463f.toInt().rgb(8),)) // 第一段
    out.addAll(hitEquipO)
    out.addAll(hitDeathRed)
    out.addAll(hitTeammate)
    out.addAll(hitSMapEnemyYellow)
    out.addAll(hitSMapEnemyBlue)
    out
}

var anaLastSMapHYFeature1Ts = 0L // 第1个boss前的门 拳头碎冰墙
var anaLastSMapHYFeature2Ts = 0L // 第1个boss击破后的卡点 踢一脚开路
var anaLastSMapHYFeature3Ts = 0L // 最后的门.往上走一步互动开门
var anaLastSMapHYFeature4Ts = 0L // 双岔路卡点.左侧有个小宝箱的直接往下右走动.
var anaLastSMapHYFeature6Ts = 0L // 双岔路卡点.最深处
var anaLastSMapHYFeature7Ts = 0L // 直通特征
var anaLastSMapHYFeature8Ts = 0L // 第1个boss击破后卡点.需要往上左微调走
var anaLastSMapHYFeature9Ts = 0L // 回音起点
var anaLastSMapHYFeature10Ts = 0L // 踢开冰桥

fun rasterizeMapHY(ss: Bitmap): Array<IntArray> {
    return rasterizeMap(ss, hitRoad)
}

fun analysisMapHY(ss: Bitmap) {
    //printLog { "analysisMapHY" }
    analysisMapElements(ss)

    val returnMapArray = rasterizeMapHY(ss)

    if (blockEliteEnemyBoss != null && mapEnemyList.isNotEmpty()) {
        // 优先Boss
        mapTargetList.add(mapEnemyList.random())
        printLog { "analyst target to boss" }
        lastSMapTargetToBossTs = System.currentTimeMillis()
        if (mapElementDistance(blockEliteEnemyBoss) < 30.pt) xBossBesideTs = System.currentTimeMillis()
    } else if (mapEnemyList.isNotEmpty()
        // 有可能冰崖跳崖了还被后面的纠缠吗？
        && (mapEnemyList.map { it.first }.average() > selfInSmallMap.x - smallMap.left - 10.pt
                || mapEnemyList.map { it.second }.average() < selfInSmallMap.y - smallMap.top - 20.pt) // 全屏幕也就140.pt左右
        && System.currentTimeMillis() - anaLastPageSceneEnterTs > 10000 // 刚开局就需要
        && System.currentTimeMillis() - anaLastSceneSettleTs > 20000
        && !bsSceneSettle && !bsSceneSettleBlacksmith) {
        // 优先敌人. 贴身的敌人-20.pt来缠斗击杀
        mapTargetList.add(mapEnemyList.random())
        printLog { "analyst target to elite enemy" }
    } else if (bsSceneSettleBlacksmith && mapBlacksmithList.isNotEmpty()) {
        mapTargetList.add(mapBlacksmithList.random())
        printLog { "analyst target to blacksmith" }
    } else if (bsSceneSettle && mapBlueDoorList.isNotEmpty()) {
        mapTargetList.add(mapBlueDoorList.random())
        printLog { "analyst target to blueDoor" }
    } else if (mapRightNotArrived.size > 0) {
        mapTargetList.add(mapRightNotArrived.random())
        printLog { "analyst target to right" }
    } else if (mapTopNotArrived.size > 0 && mapTopNotArrived.any { it.first > selfInSmallMap.x - smallMap.left + 10.pt }) {
        mapTopNotArrived.maxByOrNull { it.first }
            ?.also {
                mapTargetList.add(it)
                printLog { "analyst target to top right" }
            }
    } else if (mapSampleArrived.size > 0) {
        if (!Properties.scenePetClear && System.currentTimeMillis() - lastEliteEnemyBossTs < 12000) {
            mapSampleArrived.filter { it.second < selfInSmallMap.y - smallMap.top - 10.pt }
                .minByOrNull { it.second }?.also {
                    mapTargetList.add(it) // 不是宠物分解走上面
                    printLog { "analyst target to sample top after boss" }
                }
        } else {
            mapSampleArrived.filter { it.first > selfInSmallMap.x - smallMap.left + 10.pt }
                .maxByOrNull { it.first }?.also {
                    mapTargetList.add(it)
                    printLog { "analyst target to sample right" }
                }
        }
    }

    if (mapTargetList.isNotEmpty()) {
        val target = mapTargetList.random()
        returnMapArray[target.first][target.second] = TARGET
    }

    angleByRoad = analysisRasterizeMap(returnMapArray)
    angleByRoad = angleSpecialAdjust(angleByRoad ?: 62.shake(15))

    // map feature
    analysisMapHYIsStartPoint(ss)

    if ((blockBlueDoor == null || blockBlacksmith == null)
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 10000) {
        printLog { "analystHY $mapSimpleStatus" }

        if (System.currentTimeMillis() - lastMapTopNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && lastMapTopP == null
            && lastMapBottomP == null
            && (lastMapLeftP ?: -1f) in 0.56 .. 0.79
            && lastMapRightP == null
        ) {
            // [上(1736957284s,c:0][下(1736957284s,c:0][左(0s,0.7742475,c:23][右(1736957284s,c:0]]
            // [上(1736957333s,c:0][下(1736957333s,c:0][左(0s,0.68208843,c:23][右(1736957333s,c:0]
            // [上,29s,c:0][下,60s,c:0][左,0s,0.5769231,c:4][右,22s,c:0]][Road,c:12]
            // [上,27s,c:0][下,57s,c:0][左,0s,0.6387632,c:17][右,20s,c:0]][Road,c:14]
            printLog { "analystHY 节点.小boss前拳头碎冰墙" }
            anaLastSMapHYFeature1Ts = System.currentTimeMillis()
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs < 1000
            && (lastMapTopP ?: -1f) in 0.80 .. 0.90
            && (lastMapBottomP ?: -1f) in 0.02 .. 0.12
            && (lastMapLeftP ?: -1f) in 0.67 .. 0.77
            && (lastMapRightP ?: -1f) in 0.04 .. 0.14
        ) {
            // [上(0s,0.8530776][下(0s,0.078265205][左(0s,0.7239057][右(0s,0.09615385]
            printLog { "analystHY 节点.最后的门" }
            anaLastSMapHYFeature3Ts = System.currentTimeMillis()
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs < 1000
            && (lastMapTopP ?: -1f) in 0.57 .. 0.67
            && lastMapLeftP == null
            && lastMapRightP == null
        ) {
            // 下边偶尔有一个
            // [上(0s,0.6476998,c:28][下(4s,c:0][左(87s,c:0][右(1736957684s,c:0]]
            // [上(0s,0.60821384,c:26][下(22s,c:0][左(105s,c:0][右(1736957703s,c:0]]
            printLog { "analystHY 节点.小boss击破后踢一脚开路" }
            anaLastSMapHYFeature2Ts = System.currentTimeMillis()
        } else if (
            System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && (lastMapTopP ?: -1f) in 0.10 .. 0.24
            && (lastMapBottomP ?: -1f) in 0.40 .. 0.55
            && lastMapLeftP == null
        ) {
            // [上(0s,0.17489912,c:35][下(0s,0.4609228,c:24][左(11s,c:0][右(0s,0.30341882,c:22]]
            // [上(0s,0.18087657,c:33][下(0s,0.5005136,c:22][左(148s,c:0][右(0s,0.32498986,c:21]]
            printLog { "analystHY 节点.双岔路卡点.左侧有个小宝箱" }
            anaLastSMapHYFeature4Ts = System.currentTimeMillis()
        } else if (
            System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && (lastMapTopP ?: -1f) in 0.28 .. 0.38
            && (lastMapBottomP ?: -1f) in 0.65 .. 0.75
            && lastMapLeftP == null
            && System.currentTimeMillis() - lastBlacksmithTs > 10000
        ) {
            // [上,0s,0.33250248,c:17][下,0s,0.7057548,c:43][左,85s,c:0][右,0s,0.65799016,c:33]][Road,c:24]
            printLog { "analystHY 节点.双岔路卡点.最深处" }
            anaLastSMapHYFeature6Ts = System.currentTimeMillis()
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
                && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
                && System.currentTimeMillis() - lastMapLeftNotArrivedTs < 1000
                && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
                && (lastMapTopP ?: -1f) in 0.51 .. 0.60
                && lastMapBottomP == null
                && (lastMapLeftP ?: -1f) in 0.73 .. 0.84
                && lastMapRightP == null
        ) {
            // [上,0s,0.55273074,c:15][下,142s,c:0][左,0s,0.7777778,c:22][右,210s,c:0]][Road,c:28]
            // [上,0s,0.5412864,c:13][下,153s,c:0][左,0s,0.7606838,c:20][右,220s,c:0]][Road,c:26]
            // [上,0s,0.5776836,c:14][下,161s,c:0][左,0s,0.81493866,c:23][右,229s,c:0]][Road,c:26]
            // [上,0s,0.55273074,c:15][下,78s,c:0][左,0s,0.770979,c:22][右,44s,c:0]][Road,c:26]
            printLog { "analystHY 节点.第1个boss击破后卡点.需要往上左微调走" }
            anaLastSMapHYFeature8Ts = System.currentTimeMillis()
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && (lastMapTopP ?: -1f) in 0.82 .. 0.92
            && lastMapBottomP == null
            && lastMapLeftP == null
            && (lastMapRightP == null || (lastMapRightP ?: -1f) in 0.03 .. 0.15)
            && System.currentTimeMillis() - lastBlueDoorTs < 2000
            && System.currentTimeMillis() - xLastUnknownTs > 6000
            && System.currentTimeMillis() - xLastSceneEnterTs > 10000
            && System.currentTimeMillis() - lastBlueDoorTs < 3000
            && System.currentTimeMillis() - anaLastSMapHYFeature8Ts > 3000
        ) {
            // [上,0s,0.8701421,c:33][下,53s,c:0][左,74s,c:0][右,0s,0.066951565,c:3]][Road,c:9]
            // [上,0s,0.8717514,c:15][下,1742132613s,c:0][左,1742132613s,c:0][右,0s,0.12606838,c:4]][Road,c:7]
            printLog { "analystHY 节点.直通C" }
            anaLastSMapHYFeature7Ts = System.currentTimeMillis()
            anaClearSceneSettle()
        } else if (System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && ((lastMapBottomP ?: -1f) in 0.29 .. 0.37 || (lastMapTopP ?: -1f) in 0.55 .. 0.63)
            && lastMapLeftP == null
            && lastMapRightP == null
            && !bsSceneSettle && !bsSceneSettleBlacksmith
            && System.currentTimeMillis() - lastEliteEnemyBossTs > 8000
        ) {
            // [上,1s,c:0][下,0s,0.33050847,c:2][左,89s,c:0][右,109s,c:0]][Road,c:5]
            // [上,0s,0.59079903,c:21][下,0s,0.32344633,c:4][左,116s,c:0][右,136s,c:0]][Road,c:10]
            // [上,2s,c:0][下,2s,c:0][左,132s,c:0][右,152s,c:0]][Road,c:2]
            // 这里要么上 要么下; 下必须要
            printLog { "analystHY 节点.踢开冰桥" }
            anaLastSMapHYFeature10Ts = System.currentTimeMillis()
        }
    }




    if (System.currentTimeMillis() - anaLastPageSceneEnterTs > 8000
        && System.currentTimeMillis() - xLastUnknownTs > 8000
        && System.currentTimeMillis() - anaLastHasEnemyNameTs > 4000
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 4000
        && blockBlueDoor != null
        && blockBlacksmith != null
        && !bsSceneSettle && !bsSceneSettleBlacksmith
        && System.currentTimeMillis() - lastMapTopNotArrivedTs > 3000 // 起码x秒
        && simpleAssertSceneCompleted(blockBlueDoor, blockBlacksmith)) {
        printLog { "analyst scene completed count $anaCounterSceneSettle" }
        anaCounterSceneSettle++
        if (anaCounterSceneSettle > 0) {
            printLog { "analyst scene completed" }
            anaMarkSceneSettle()
        }
    }
    if (System.currentTimeMillis() - lastBlueDoorTs > 3000
        && System.currentTimeMillis() - lastBlacksmithTs > 3000) {
        anaCounterSceneSettle = 0
    }
}

private val cHYP1StartA1 by lazy { Block("回音起点黑上1", selfInSmallMap.x - 60.pt, selfInSmallMap.y - 20.pt, 20.pt, 20.pt) }
private val cHYP1StartA2 by lazy { Block("回音起点黑上2", selfInSmallMap.x - 24.pt, selfInSmallMap.y - 44.pt, 20.pt, 20.pt) }
private val cHYP1StartA3 by lazy { Block("回音起点黑上3", selfInSmallMap.x + 20.pt, selfInSmallMap.y - 80.pt, 20.pt, 20.pt) }
private val cHYP1StartA4 by lazy { Block("回音起点黑下4", selfInSmallMap.x + 40.pt, selfInSmallMap.y + 0.pt, 30.pt, 20.pt) }
private val cHYP1StartA5 by lazy { Block("回音起点黑下5", selfInSmallMap.x + 80.pt, selfInSmallMap.y - 20.pt, 45.pt, 20.pt) }
private val cHYP1StartA6 by lazy { Block("回音起点门路6", selfInSmallMap.x + 70.pt, selfInSmallMap.y - 70.pt, 22.pt, 22.pt) }
private val hitHYStartRoad by lazy { setOf(0xff46453F.toInt().rgb(5),) }

fun analysisMapHYIsStartPoint(ss: Bitmap): Boolean{
    if (blockBlacksmith == null
        && System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
        && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
        && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 10000) {
        var hitPart = 0
        // hit [hySp6] 1.0 hitCount 36 total 36
        if (!Analysis.hit(ss, cHYP1StartA1, hitHYStartRoad, 4.pt, 0.1f, )) hitPart++ // , "hySp1" // 0.5
        if (!Analysis.hit(ss, cHYP1StartA2, hitHYStartRoad, 4.pt, 0.1f, )) hitPart++ // , "hySp2" // 0.5
        if (!Analysis.hit(ss, cHYP1StartA3, hitHYStartRoad, 4.pt, 0.1f, )) hitPart++ // , "hySp3" // 0.5
        if (!Analysis.hit(ss, cHYP1StartA4, hitHYStartRoad, 4.pt, 0.1f, )) hitPart++ // , "hySp4" // 0.5
        if (!Analysis.hit(ss, cHYP1StartA5, hitHYStartRoad, 4.pt, 0.1f, )) hitPart++ // , "hySp5" // 0.5
        if (Analysis.hit(ss, cHYP1StartA6, hitHYStartRoad, 4.pt, 0.5f, )) hitPart++ // , "hySp6" // 0.5
        if (hitPart >= 6) {
            printLog { "analystHY 节点.回音路线是起点B" }
            anaLastSMapHYFeature9Ts = System.currentTimeMillis()
            anaClearSceneSettle()
            return true
        }
    }

    // 如果顶端是精英怪物可能翻车
    if (
        System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
        && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
        && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
        && (lastMapTopP ?: -1f) in 0.83 .. 0.91
        && lastMapBottomP == null
        && lastMapLeftP == null
        && (lastMapRightP == null || (lastMapRightP ?: -1f) in 0.04 .. 0.11)
        && lastMapRightC in 0 .. 8
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 10000
        && (blockBlueDoor == null || blockBlacksmith == null)
//          第1帧传送门没有.互动倒是可能有  && System.currentTimeMillis() - lastBlueDoorTs < 6000
    ) {
        // 真720p [上,0s,0.8652139,c:21][下,1748862620s,c:0][左,1748862620s,c:0][右,1748862620s,c:0]][Road,c:5]
        // [上,0s,0.88476527,c:29][下,1748860035s,c:0][左,1748860035s,c:0][右,0s,0.072649576,c:1]][Road,c:13]
        // [上,0s,0.8884181,c:28][下,174s,c:0][左,264s,c:0][右,0s,0.072649576,c:1]][Road,c:12]
        printLog { "analystHY 节点.回音路线是起点C" } // 这个判断很准确
        anaLastSMapHYFeature9Ts = System.currentTimeMillis()
        anaClearSceneSettle()
        return true
    }
    return false
}

private fun simpleAssertSceneCompleted(blueDoor: Block?, blacksmith: Block?): Boolean {
    if (blueDoor == null) return false
    if (blacksmith == null) return false
    val edge = 80.pt
    return blueDoor.top < smallMap.bottom - 40.pt
            && blueDoor.right > smallMap.left + 60.pt
            && abs(blueDoor.x - blacksmith.x) < edge
            && abs(blueDoor.y - blacksmith.y) < edge
}

private fun angleSpecialAdjust(angle: Int): Int {

    if (System.currentTimeMillis() - lastMapTopNotArrivedTs > 3000
        && System.currentTimeMillis() - anaLastHasEnemyBossNameTs in 1000 .. 3000
        && System.currentTimeMillis() - lastEliteEnemyBossTs < 1000
        && System.currentTimeMillis() - xLastAtkTs < 10000
        && System.currentTimeMillis() - anaLastSMapHYFeature1Ts > 20000) {
        printLog { "analystHY 躲避最终Boss秒杀A" }
        return (angle + 180) % 360 // 往左外偏 // 激光
    }
    if (System.currentTimeMillis() - lastMapTopNotArrivedTs > 3000
        && System.currentTimeMillis() - anaLastHasEnemyBossNameTs in 3000 .. 15000
        && System.currentTimeMillis() - lastEliteEnemyBossTs < 1000
        && System.currentTimeMillis() - xLastAtkTs < 15000
        && System.currentTimeMillis() - anaLastSMapHYFeature1Ts > 20000) {
        printLog { "analystHY 躲避最终Boss秒杀B" }
        return (angle + 300) % 360 // 往左外偏 // 激光
    }

    if (angle in 95 .. 280) {
        if (System.currentTimeMillis() - xLastAtkTs > 5000
            && System.currentTimeMillis() - lastEliteEnemyBossTs > 5000
            && System.currentTimeMillis() - lastEliteEnemyTs > 5000
            && System.currentTimeMillis() - anaLastSMapHYFeature1Ts > 20000
            && System.currentTimeMillis() - anaLastSMapHYFeature2Ts > 20000
            && System.currentTimeMillis() - anaLastSMapHYFeature3Ts > 20000
            && System.currentTimeMillis() - anaLastSMapHYFeature4Ts > 20000
            && System.currentTimeMillis() - anaLastSMapHYFeature6Ts > 20000
            && System.currentTimeMillis() - xLastSceneEnterTs > 30000
            && System.currentTimeMillis() - xLastPetBlacksmithTs > 20000
            && System.currentTimeMillis() - xLastUnknownTs < 20000) {
            printLog { "analystHY angleSpecialAdjust $angle to 40" }
            return 40
        }
    }
    return angle
}
